package com.gz.web;

import com.gz.web.requestParam.entity.Master;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RequestMapping(value = "/vote")
@Controller
public class VoteHandler {

    @RequestMapping(value = "/vote01/{id}",method = RequestMethod.DELETE)
    public String delete(@PathVariable("id") String id) {
        System.out.println("删除id为"+id+"的书籍");
        //因为return只接收GET或POST请求，求delete和put都得用重定向来转到目标页面
        //这里/包含了工作路径
        return "redirect:/vote/success";
    }

    @RequestMapping(value = "/success")
    public String success() {
        return "success";
    }

    @RequestMapping(value = "/vote01/{id}",method = RequestMethod.PUT)
    public String update(@PathVariable("id") String id) {
        System.out.println("修改id为"+id+"的书籍");
        //因为return只接收GET或POST请求，求delete和put都得用重定向来转到目标页面
        //这里/包含了工作路径
        return "redirect:/vote/success";
    }

    @RequestMapping(value = "/vote01")
    public String test01(@RequestParam("name") String username) {
        System.out.println("username= "+username);//username= gz
        return "success";
    }

    @RequestMapping(value = "/vote02")
    public String test02(@RequestHeader("Accept-Encoding")String ae,
                         @RequestHeader("Host")String host) {
        System.out.println("Accept-Encoding= "+ae);//Accept-Encoding= gzip, deflate, br, zstd
        System.out.println("Host= "+host);//Host= localhost:8080
        return "success";
    }

    @RequestMapping(value = "/vote03")
    public String test03(Master master) {
        System.out.println("master= " + master);
        return "success";
    }

    //使用servlet api来获取提交的数据
    @RequestMapping(value = "/vote04")
    public String test04(HttpServletRequest request,
                         HttpServletResponse response) {
        String username = request.getParameter("username");
        String pwd = request.getParameter("pwd");
        System.out.println("username= "+username+" pwd= "+pwd);
        return "success";
    }

    //演示将提交的数据->springmvc封装到java对象->springmvc会自动将数据放入到request域
    @RequestMapping(value = "/vote05")
    public String test05(Master master, Map<String,Object> map) {
        //添加属性到request域中
        //方式1.request.set("address","?")
        //方式2.通过map对象添加
        //  原理:springmvc会遍历map中的k-v，存放到request域

        return "vote_ok";
    }

    @RequestMapping(value = "/vote06")
    public ModelAndView test05(Master master) {
        //方式3.通过返回ModelAndView对象，将输入放入request域中
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("address","shanghai");
        //这里指定跳转的视图名称
        modelAndView.setViewName("vote_ok");
        //注意这里默认已经将master放入进去了，如果这里再次add master会覆盖
//        modelAndView.addObject("master", master);
        return modelAndView;
    }

    //当Handler的方法被标识为@ModelAttribute，就视为前置方法
    //全局
    @ModelAttribute
    public void prepareModel() {
        System.out.println("prepareModel() ---- 完成准备工作");
    }
}
